import {request, writeLog} from "@/libs/core.js"
import { Message } from 'element-ui'
/**
强类型，所以传递参数的时候要注意
@param args{account:xxx. loginpw:xxx, vcode:xxxx}
****************************************************************/
const getList = async(args) => {
  args.status = parseInt(args.status);
  const data = await request({url:'/api/core/role/list', data:args, method:'post'});
  if (data && data.code == 0 ) {
    return data.datas;
  }
  return false;
}

const doDelete = async(args) => {
	const data = await request({url:'/api/core/role/delete', data:args, method:'post'});
	if(data && data.code == 0){
		Message({message: '角色信息删除成功', type: 'success', duration: 3 * 1000});
		return true;
	}
  return false;
}

const doSave = async(args) => {
	const data = await request({url:'/api/core/role/update', data:args, method:'post'});
	if(data && data.code == 0){
		if(!args.id){
			Message({message: '角色信息新增成功', type: 'success', duration: 3 * 1000});
		}else{
			Message({message: '角色信息编辑成功', type: 'success', duration: 3 * 1000});
		}
		return true;
	}
  return false;
}

const getAccess = async(args) => {
    var access = []
    const data = await request({url:'/api/core/role/getaccess', data:args, method:'post'});
    if (data && data.code == 0 && data.datas.access) {
        for (let idx = 0; idx < data.datas.access.length; idx++) {
            access.push(parseInt(data.datas.access[idx]));
        }
    }
    return access;
}

const setAccess = async(args) => {
	const data = await request({url:'/api/core/role/setaccess', data:args, method:'post'});
  if (data && data.code == 0 ) {
  	Message({message: '角色授权处理成功', type: 'success', duration: 3 * 1000});
    return true;
  }
  return false;
}

export default {
	getList,doDelete,doSave,getAccess,setAccess
};
